package com.cn.wanxi.servlet;

import com.alibaba.fastjson.JSONObject;
import com.cn.wanxi.model.CompanyModel;
import com.cn.wanxi.model.NavModel;
import com.cn.wanxi.model.ProductModel;
import com.cn.wanxi.service.CompanyService;
import com.cn.wanxi.service.NavService;
import com.cn.wanxi.service.ProductService;
import com.cn.wanxi.service.impl.CompanyServiceImpl;
import com.cn.wanxi.service.impl.NavServiceImpl;
import com.cn.wanxi.service.impl.ProductServiceImpl;
import com.cn.wanxi.vo.HomeModel;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/company")
public class CompanyServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        1.乱码
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
//        2.得到签到传递过来的数据


//        3.将这些封装成对象
        ProductModel model = new ProductModel();
//        首页默认isShow为1
        model.setWhereShow(2);
        model.setPage(1);
        model.setLimit(8);

//        4.调用服务逻辑层
        CompanyService companyService = new CompanyServiceImpl();
        NavService navService = new NavServiceImpl();
        ProductService productService = new ProductServiceImpl();
//        5.得到结果信息
        HomeModel homeModel = new HomeModel();
//        得到公司信息
        CompanyModel companyModel = companyService.getCompanyModel();
//       得到导航信息
        List<NavModel> navModelList = navService.getNavModelList();
//        得到产品信息
        List<ProductModel> productModelList = productService.getProductModelList(model);
//        第三种方式：第四阶段（推荐）
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("companyModel", companyModel);
        jsonObject.put("navModelList", navModelList);
        jsonObject.put("productModelList", productModelList);

        resp.getWriter().println(jsonObject);

    }
}
